package com.zhiy.com.estation.api;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * All Resered by My Zong  @2016-2017
 */
public class Api {
    private Retrofit mRetrofit;
    public ApiService mService;

    private Api(){
//        用来打印json字符串的log日志信息
        HttpLoggingInterceptor mInterceptor = new HttpLoggingInterceptor();
//        body 会展示请求内容和响应内容
        mInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(mInterceptor)
                .build();
        mRetrofit = new Retrofit.Builder()
                .client(client)
//                支持rxjava
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                支持gson解析
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(ApiUrl.BASE_URL).build();

        mService = mRetrofit.create(ApiService.class);
    }
    public static class SingletonHolder {
        private static final Api Instance = new Api();
    }
    public static Api getInstance(){
        return SingletonHolder.Instance;
    }
}
